Extract ErrorCode.Create() recognition into ErrorCodeFacts utility#38
Merged
Merged
Conversation
(b) Factor the ErrorCode.Create("literal") recognition and the
Optional<object?>.ConstantValue handling shared by FCE001, FCE002, FCE004
and FCE005 into a single ErrorCodeFacts helper. FCE004/FCE005 previously
relied on Optional<object?>.Value defaulting to null when HasValue is
false; the semantics are now centralized and made explicit, and each
analyzer no longer repeats the static-Create / arity / constant checks.
Behavior is unchanged for all four rules.
(a) FCE016 now also reports `_ = error.ToException();`. The explicit
discard wraps the invocation in an ISimpleAssignmentOperation whose target
is an IDiscardOperation, so the previous "bare expression statement" guard
missed it even though the result is thrown away just the same. ToException()
is a pure builder, so an explicit discard is as pointless as a standalone
call. Added a unit test and kept the FCE016 documentation (EN + FR) in sync.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019NBsUT3XFzH86c8L9iSDmn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Centralizes the logic for recognizing
ErrorCode.Create("...")invocations and extracting literal codes into a newErrorCodeFactsutility class. This eliminates code duplication across four error-code analyzers (FCE001, FCE002, FCE004, FCE005) and improves maintainability.Type of change
Changes
Added
ErrorCodeFacts.cs: New utility class with three public methods:GetCreateArgument(): RecognizesErrorCode.Create(x)invocations and returns the argument operation, or null if not a valid callTryGetNonEmptyLiteralCode(): Extracts the code when the argument is a non-empty literal string constantIsBlankLiteralCode(): Checks if the argument is a literal constant with null, empty, or whitespace valueErrorCodeMetadataNameconstant previously duplicated across analyzersRefactored four analyzers to use
ErrorCodeFacts:DuplicateErrorCodeAnalyzerEmptyErrorCodeAnalyzerInvalidErrorCodeFormatAnalyzerTooGenericErrorCodeAnalyzerEach now delegates
ErrorCode.Create()recognition and literal code extraction to the utility, reducing boilerplate and improving consistency.Enhanced
UnusedToExceptionResultAnalyzer: Now detects whenToException()result is explicitly discarded with_ =assignment, in addition to standalone statements.Added test case for FCE016:
Reports_when_result_is_assigned_to_a_discard()verifies the analyzer catches explicit discard patterns.Updated FCE016 documentation (English and French): Clarified that the rule now covers both standalone statements and explicit discards with
_ =.Testing
dotnet build FirstClassErrors.slndotnet test FirstClassErrors.slnFirstClassErrors.Analyzers.UnitTests)Documentation
doc/updateddoc/README.fr.md) updated if user-facing behavior changed